/**
 * 
 */
package com.tw.trips.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.springframework.stereotype.Service;

/**
 * @author M1005264
 *
 */
@Service
public class IrctcStationCodes {

	private static final String STATION_CODES_FILE ="stationcodes.properties"; 

	private Map<String, String> stationCodesMap = new HashMap<String, String>();

	public String getLocationFromCode(String code){

		if(stationCodesMap.isEmpty()){
			loadStationCodes();
		}
		return stationCodesMap.get(code);
	}

	private void loadStationCodes() {
		try{  
			InputStream is = IrctcStationCodes.class.getClassLoader().getResourceAsStream(STATION_CODES_FILE);  
			Properties prop = new Properties();  
			prop.load(is);
			for(Entry<Object, Object> entry :prop.entrySet()){
				String name = (String) entry.getValue();
				String code = (String) entry.getKey();
				stationCodesMap.put(code, name);
			}
//			System.out.println(stationCodesMap.get("KPD"));
			is.close();  
			/* code to use values read from the file*/  
		}catch(Exception e){  
			System.out.println("Failed to read from " + STATION_CODES_FILE + " file.");  
		}  
	}
	
	public static void main(String[] args) {
		IrctcStationCodes code = new IrctcStationCodes();
//		code.getLocationFromCode("MAS");
		System.out.println(code.getLocationFromCode("MAS"));
		
	}
}
